package com.ddwl.search.service;

import com.ddwl.common.feign.api.GoodsApi;
import com.ddwl.common.util.BeanConverUtils;
import com.ddwl.schema.bo.base.PageBo;
import com.ddwl.schema.bo.search.GoodsSearchBo;
import com.ddwl.schema.vo.base.PageVo;
import com.ddwl.schema.vo.base.RestVo;
import com.ddwl.schema.vo.goods.CategoryVo;
import com.ddwl.schema.vo.goods.ItemVo;
import com.ddwl.schema.vo.search.EsItemVo;
import com.ddwl.search.esrepository.EsProductRepository;
import com.ddwl.search.esrepository.domain.EsItem;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;


/**
 * 商品搜索管理Service实现类
 * Created by macro on 2018/6/19.
 */
@Slf4j
@Service
public class EsProductServiceImpl {


    @Autowired
    private EsProductRepository productRepository;
    @Resource
    private GoodsApi goodsApi;

    /**
     * 向ES导入数据商品数据
     * @return
     */
    public int importAll(){
        boolean b = true;
        Integer page = 1;
        Integer size = 500;
        int result = 0;
        while (b){
            PageBo pageBo = new PageBo();
            pageBo.setPageSize(size);
            pageBo.setPage(page);
            PageVo<ItemVo> esProductList = goodsApi.queryItemAll(pageBo).getResult();
            if(!CollectionUtils.isEmpty(esProductList.getData())){
                List<EsItem> esItems = BeanConverUtils.convertBeanList(esProductList.getData(), EsItem.class);
                Iterable<EsItem> esProductIterable = productRepository.saveAll(esItems);
                Iterator<EsItem> iterator = esProductIterable.iterator();
                while (iterator.hasNext()) {
                    result++;
                    iterator.next();
                }
                page+=1;
            }else {
                b=false;
            }
        }
        return result;
    }


    public void delete(String id) {
        productRepository.deleteById(id);
    }

    public EsItem create(String id) {
        EsItem result = null;
        RestVo<ItemVo>  restVo = goodsApi.getItemInfo(id);
        if(restVo.getResult()!=null){
            RestVo<CategoryVo> categoryVoRestVo = goodsApi.getCategoryDetail(restVo.getResult().getCategoryId());
            EsItem esItem = BeanConverUtils.convertBean(restVo.getResult(),EsItem.class);
            if(categoryVoRestVo.getResult()!=null){
                esItem.setCategoryName(categoryVoRestVo.getResult().getName());
            }
            result = productRepository.save(esItem);
        }
        return result;
    }

    public ItemVo create(ItemVo itemVo) {
        if(itemVo!=null){
            RestVo<CategoryVo> categoryVoRestVo = goodsApi.getCategoryDetail(itemVo.getCategoryId());
            EsItem esItem = BeanConverUtils.convertBean(itemVo,EsItem.class);
            if(categoryVoRestVo.getResult()!=null){
                esItem.setCategoryName(categoryVoRestVo.getResult().getName());
            }
            productRepository.save(esItem);
        }
        return itemVo;
    }

    public void delete(List<String> ids) {
        if (!CollectionUtils.isEmpty(ids)) {
            List<EsItem> esProductList = new ArrayList<>();
            for (String id : ids) {
                EsItem esProduct = new EsItem();
                esProduct.setId(id);
                esProductList.add(esProduct);
            }
            productRepository.deleteAll(esProductList);
        }
    }

    public PageVo<EsItemVo> search(PageBo<GoodsSearchBo> pageBo) {
        return productRepository.search(pageBo);
    }
}
